package org.jeecg.modules.demo.image;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.MinioUtil;

import org.jeecg.modules.demo.image.entity.UploadImage;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * 功能描述
 *
 * @program: jeecg-boot
 * @description:
 * @author: wwq
 * @create: 2024-05-09 10:16
 **/

@Api(value="附件上传")
@RestController
@RequestMapping("/zyUploadAnnex")
@Slf4j
@RequiredArgsConstructor
public class ImageController {




    @AutoLog(value = "附件上传")
    @ApiOperation(value="附件上传", notes="附件上传")
    @PostMapping(value = "/uploadAnnex")
    public Result<UploadImage> uploadAnnex(MultipartFile file) throws Exception {
        String filename=file.getOriginalFilename();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
//        String upload = OssBootUtil.upload1(file,filename.substring(filename.lastIndexOf(".")));
        //TODO
        String upload =MinioUtil.upload(file, "file",null);

        UploadImage uploadImage=new UploadImage();
        uploadImage.setName(filename);
        uploadImage.setUrl(upload);
        uploadImage.setSize(processFile(file));
        uploadImage.setCreateTime(timestamp);
        uploadImage.setType(file.getContentType());
        uploadImage.setPeople(loginUser.getRealname());
        //我需要判断该文件是否未照片

        return Result.ok(uploadImage);
    }

    public String processFile(MultipartFile file) {
        long fileSize = file.getSize(); // 获取文件大小，单位为字节
        if (fileSize < 1024) { // 文件大小小于 1KB
            double fileSizeB = fileSize * 1.0; // 将文件大小转换为 B
            System.out.println("文件大小：" + String.format("%.2f", fileSizeB) + " B");
            return fileSizeB+"B";
        } else if (fileSize < 1024 * 1024){ // 文件小于1Mb
            double fileSizeKB = fileSize / 1024.0; // 将文件大小转换为 KB
            System.out.println("文件大小：" + String.format("%.2f", fileSizeKB) + " KB");
            return String.format("%.2f", fileSizeKB)+"KB";
        }else {
            double fileSizeMB = fileSize / (1024.0 * 1024.0); // 将文件大小转换为 MB
            System.out.println("文件大小：" + String.format("%.2f", fileSizeMB) + " MB");
            return String.format("%.2f", fileSizeMB)+"MB";
        }
    }





}
